<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/24
 * Time: 14:51
 */

namespace app\common\lib;

use app\admin\controller\Exception;
use WeChatDeveloper\app\WxMsg as Msg;
use think\Db;

//小程序消息推送接口
class MiniMsg
{

    public $content;//消息内容
    public $data;//sh
    public $condition;//触发条件
    public $touser;//接收用户openid
    public $template_id;//所需下发的模板消息的id
    public $url;//点击模板卡片后的跳转页面，仅限本小程序内的页面。支持带参数,（示例index?foo=bar）。该字段不填则模板无跳转。
    public $form_id;//表单提交场景下，为 submit 事件带上的 formId；支付场景下，为本次支付的 prepay_id
    public $color;//模板颜色
    //每添加一个条信息都需要一个手动添加触发条件

    /**
     * MiniMsg constructor.
     * @param $config array 传 object [1:会员,3:门店] condition:[[1,2,3,4,5,6],[1,2,3]]
     * ["object"=>1,"condition"=>3,"content"=>[22,33]]
     */
    public function __construct($config)
    {
        $this->condition = intval($config["condition"]);
        $this->content = $config["content"] ?: [];
        $this->touser = $config["touser"];//openid
        //$this->template_id = $config["template_id"];//模板id
//        if (isset($config["color"]) && !empty($config["color"])) {
//            $this->color = $config["color"];
//        }

    }

    /**
     * @name 小程序消息模板推送
     * @throws
     * @return
     */
    public function sendMsg()
    {
        $config = [
            'appid' =>  config('wx.appid'),//微信公众号appid
            'appsecret' =>  config('wx.appsecret')//微信公众号appsecret
        ];
        $msg = new Msg($config);
        $data = $this->getCondition();
        //$url_arr = $this->getTempleUrl($this->object);
        $cons = "";
        $cons1 = "";
        $content = $this->content;
        if ($content) {
            ///foreach ($content as $k => $v) {
//                if (substr_count($data[$this->condition]["data"]["keyword1"]["value"], '*') > 0) {
//                    $cons1 .= substr_replace($data[$this->condition]["data"]["keyword1"]["value"], $v, strpos($data[$this->condition]["data"]["keyword1"]["value"], "*"), 1);
//                    $data[$this->condition]["keyword1"]["value"] = $cons1;
//                    $cons1 = "";
//                } else {
//                    if (substr_count($data[$this->condition]["data"]["keyword2"]["value"], '*') > 0) {
//                        $cons .= substr_replace($data[$this->condition]["data"]["keyword2"]["value"], $v, strpos($data[$this->condition]["data"]["keyword2"]["value"], "*"), 1);
//                        $data[$this->condition]["data"]["keyword2"]["value"] = $cons;
//                        $cons = "";
//                    }
//                }
                file_put_contents("msg_send.txt",json_encode($content,JSON_UNESCAPED_UNICODE),FILE_APPEND);

                if(substr_count($data[$this->condition]["data"]["keyword1"]["value"], '*') == 1){
                    $data[$this->condition]["data"]["keyword1"]["value"] = $content[0];
                }
                if(substr_count($data[$this->condition]["data"]["keyword2"]["value"], '*') == 1){
                    $data[$this->condition]["data"]["keyword2"]["value"] = $content[1];
                }
                if(substr_count($data[$this->condition]["data"]["keyword3"]["value"], '*') == 1){
                    $data[$this->condition]["data"]["keyword3"]["value"] = $content[2];
                }
                if(substr_count($data[$this->condition]["data"]["keyword4"]["value"], '*') == 1){
                    $data[$this->condition]["data"]["keyword4"]["value"] = $content[3];
                }
                if(substr_count($data[$this->condition]["data"]["keyword5"]["value"], '*') == 1){
                    $data[$this->condition]["data"]["keyword5"]["value"] = $content[4];
                }

            //}
        }
        $info = [
            "touser" => $this->touser,
            "template_id" => $data[$this->condition]["template_id"],
            "url" => config("app.sys_domain")."/index/index/index",
            "data" => $data[$this->condition]["data"]
        ];
        try {
            return $msg->send($info);
        }catch (\Exception $e){
            $filename = ROOT_PATH . '/log/temp_msg/' . date('Ymd') . '.log';
            $str = PHP_EOL.date('Y-m-d H:i:s', time()).'--极光推送信息:'.json_encode($info).', res = fail('.$e->getMessage().')'.PHP_EOL;
            file_put_contents($filename, $str . PHP_EOL, FILE_APPEND);
            //file_put_contents("mini_msg.txt",$e->getMessage().",".json_encode($info,JSON_UNESCAPED_UNICODE));

        }
    }

    private function getCondition()
    {
        $output = [
            1 => [
                "data"=>[
                        "first"=>[
                            "value" => "尊敬的客户，您的资料已审核失败！",
                        ],
                        "keyword1" => [
                            "value" => date("Y-m-d H:i:s",time()),
                        ],
                        "keyword2" => [
                            "value" => "审核失败",
                        ],

                        "remark"=>[

                            "value"=>"感谢您的支持",
                        ]

                    ],
                "template_id"=>config("template.register_temp")
            ],//注册审核驳回
            2 => [
                "data"=>[
                        "first"=>[
                            "value" => "尊敬的客户，您的资料已审核成功！",
                        ],
                        "keyword1" => [
                            "value" => date("Y-m-d H:i:s",time()),
                        ],
                        "keyword2" => [
                            "value" => "审核成功"
                        ],
                        "remark"=>[

                            "value"=>"感谢您的注册",
                        ]
                    ],
                "template_id"=>config("template.register_temp")
            ],//注册审核成功
            3 => [
                "data"=>[
                        "first"=>[
                            "value" => "尊敬的客户，您的身份证审核通过！",
                        ],
                        "keyword1" => [
                            "value" => "*",
                        ],
                        "keyword2" => [
                            "value" => "*",
                        ],
                        "keyword3" => [
                            "value" => date("Y-m-d H:i:s",time()),
                        ],
                        "remark"=>[

                            "value"=>"感谢您的提供",
                        ]
                    ],
                  "template_id"=>config("template.auth_pass_temp")
            ],//身份审核通过
            4 => [
                "data"=>[
                        "first"=>[
                            "value" => "尊敬的客户，您的身份证审核失败！",
                        ],
                        "keyword1" => [
                            "value" => "*",
                        ],

                        "keyword2" => [
                            "value" => "*",
                        ],

                        "keyword3" => [
                            "value" => date("Y-m-d H:i:s",time()),
                        ],

                        "remark"=>[

                            "value"=>"感谢您的提供",
                        ]
                    ],
                "template_id"=>config("template.auth_no_temp")
            ],//身份审核驳回
            5  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的银行卡修改成功！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],

                    "remark"=>[

                        "value"=>"感谢您的提供",
                    ]
                ],
                "template_id"=>config("template.bank_edit_temp")
            ],//银行卡信息修改成功
            6  =>[
            "data"=>[
                "first"=>[
                    "value" => "尊敬的客户，您的银行卡修改失败！",
                ],
                "keyword1" => [
                    "value" => "*",
                ],
                "keyword2" => [
                    "value" => "*",
                ],

                "remark"=>[
                    "value"=>"感谢您的提供",
                ]
            ],
            "template_id"=>config("template.bank_edit_temp")
            ],//银行卡信息修改失败
            7  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的派客账户已恢复正常！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],

                    "keyword2" => [
                        "value" => date("Y-m-d H:i:s",time()),
                    ],

                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.user_acount_temp")
            ],//派客账户恢复正常
            8  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的派客账户已被禁用！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => date("Y-m-d H:i:s",time()),
                    ],

                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.user_disable_temp")
            ],//派克账号禁用
            9  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的接单时间超时了！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],

                    "keyword4" => [
                        "value" => "*",
                    ],

                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_overtime_temp")
            ],//接单失败—超时
            10  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，接单失败—资料未通过审核！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => date("Y-m-d H:i:s",time()),
                    ],

                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_nodata_temp")
            ],//接单失败—资料未通过审核
            26  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，资料未通过审核！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => date("Y-m-d H:i:s",time()),
                    ],

                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_nodata_temp")
            ],//资料未通过审核
            11  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您已接单成功！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_success_temp")
            ],//接单成功
            12  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，商家打款成功！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.pay_notice_temp")
            ],//已打款通知
            13  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，商家打款成功，请您及时下单付款！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.pay_neednotice_temp")
            ],//催付款通知
            14  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，该订单完成！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => "*",
                    ],
                    "keyword5" => [
                        "value" => date("Y-m-d H:i:s",time()),
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_finished_temp")
            ],//订单完成通知
            15  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，该订单已取消！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => date("Y-m-d H:i:s",time()),
                    ],
                    "keyword5" => [
                        "value" =>"*"
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_cancle_temp")
            ],//订单取消通知
            16 =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，获取平台补偿的金豆",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_mibu_temp")


            ],//金豆补偿通知
            17  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的金豆提现到账啦！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.bean_cashed_pass_temp")
            ],//金豆提现到账
            18  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的金豆提现失败！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.bean_cashed_no_temp")
            ],//金豆提现失败
            19  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的订单已经出库了！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => "*",
                    ],
                    "keyword5" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.out_treasury_temp")
            ],//需要出库的商品才需要
            20  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的订单已是待好评状态！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => "*",
                    ],
                    "keyword5" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_update_temp")
            ],//待好评通知（非必要）
            21  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的订单已是好评状态！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => "*",
                    ],
                    "keyword5" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_update_temp")
            ],//好评通过（非必要）
            22  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的订单已是待追评状态！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => "*",
                    ],
                    "keyword5" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_update_temp")
            ],//待追评通过（非必要）
            23  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的订单已是追评状态！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => "*",
                    ],
                    "keyword5" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_update_temp")
            ],//追评通过（非必要）
            24  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的订单已是追加晒图状态！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => "*",
                    ],
                    "keyword5" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_update_temp")
            ],//指定追加晒图（非必要）
            25  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的订单已是追加晒图通过状态！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => "*",
                    ],
                    "keyword5" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_update_temp")
            ],//指定追加晒图通过（非必要）
            27  =>[
                "data"=>[
                    "first"=>[
                        "value" => "尊敬的客户，您的订单为隔日单请尽快上传图片！",
                    ],
                    "keyword1" => [
                        "value" => "*",
                    ],
                    "keyword2" => [
                        "value" => "*",
                    ],
                    "keyword3" => [
                        "value" => "*",
                    ],
                    "keyword4" => [
                        "value" => "*",
                    ],
                    "keyword5" => [
                        "value" => "*",
                    ],
                    "remark"=>[
                        "value"=>"感谢您的参与",
                    ]
                ],
                "template_id"=>config("template.order_update_temp")
            ]//指定追加晒图通过（非必要）




        ];


        return $output;
    }

//消息模板
//    private function getTempleUrl($obj){
//        $pagepath = [];
//
//        switch ($obj) {
//
//            case 1:
//
//                $pagepath = [
//
//                    1=>"/pages/coupon/index",
//
//                    2=>"/pages/orders/index",
//
//                    3=>"/pages/orders/index",
//
//                    4=>"/pages/orders/index",
//
//                    5=>"/pages/traceLog/index",
//
//
//                    6=>"/pages/orders/index",
//
//
//                    7=>"/pages/orders/index",
//
//
//                    8=>"/pages/orders/index",
//
//
//                    10=>"/pages/traceLog/index",
//
//
//                    11=>"/pages/traceLog/index"
//
//
//                ];
//
//                break;
//            case 3:
//
//                $pagepath = [
//
//                    1=>"/pages/incomeDetail/index",
//
//                    2=>"/pages/incomeDetail/index",
//
//
//                    3=>"/pages/incomeDetail/index",
//
//
//                    5=>"/pages/incomeDetail/index",
//
//
//                    9=>"/pages/incomeDetail/index",
//
//
//                    10=>"/pages/incomeDetail/index",
//
//                    11=>"/pages/incomeDetail/index",
//
//
//                    12=>"/pages/incomeDetail/index",
//
//
//                    13=>"/pages/incomeDetail/index"
//
//
//
//                ];
//
//
//                break;
//
//        }
//
//        return $pagepath;
//
//    }


}
